﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Security.Cryptography;

public partial class User_Login : System.Web.UI.Page
{
    CatalogBUS cataBUS = new CatalogBUS();
    DataTable tbCatalog = new DataTable();
    DataView viewCatalog;
    MemberBUS memBUS = new MemberBUS();
    DataTable tbMem = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUser.Text.Equals("") || txtPassword.Text.Equals(""))
        {
            lblError.Text = "You must enter password and username";
        }
        else
        {
            tbMem = memBUS.isvalidLogin(txtUser.Text, Md5AddSecret(txtPassword.Text));
            if (tbMem.Rows.Count > 0)
            {
                Session["ID"] = tbMem.Rows[0]["M_ID"];
                Session["User"] = tbMem.Rows[0]["M_UserName"];
                Response.Redirect("Home.aspx");
            }
            else
            {
                lblError.Text = "Wrong Password";
                txtPassword.Attributes.Add("value", "");
            }
        }

    }
    public string Md5AddSecret(string strChange)
    {
        //Change the syllable into UTF8 code
        byte[] pass = Encoding.UTF8.GetBytes(strChange);
        MD5 md5 = new MD5CryptoServiceProvider();
        string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
        return strPassword;
    }
}